﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"Snake.h"
//定位光标函数
void SetPos(short x,short y) {

	//获取句柄
	HANDLE hOutput = NULL;
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);

	//设置标准输出上光标的位置为pos
	COORD pos = { x, y };
	SetConsoleCursorPosition(hOutput, pos);

}

//打印欢迎界面
WelcomeToGame() {

	SetPos(35,13);
	wprintf(L"欢迎来到贪吃蛇小游戏");
	SetPos(36, 17);
	system("pause");

	system("cls");

	SetPos(24, 13);
	wprintf(L"按↑ . ↓ . ← . → 来控制蛇的移动，F3加速 ，F4减速");
	SetPos(35, 14);
	wprintf(L"加速能够得到更高的分数");
	SetPos(36, 17);
	system("pause");

	system("cls");

}

//绘制地图
CreateMap() {

	//上墙
	for (int i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}

	//下墙
	SetPos(0,26);
	for (int i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	
	//左墙
	for (int i = 1; i <= 25; i++)
	{
		SetPos(0,i);
		wprintf(L"%lc", WALL);
	}

	//右墙
	for (int i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}
}

//创建一条贪吃蛇，并且部分初始化
void InitSnake(pSnake ps) {
	//初始化蛇头方向，一个食物分数，总分数，屏幕休眠时间，蛇状态
	ps->_dir = RIGHT;
	ps->_food_weight = 10;
	ps->_score = 0;
	ps->_sleep_time = 200;
	ps->_status = OK;

	//初始化蛇身
	pSnakeNode cur = NULL;

	for (int i = 0; i < 5; i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));//创建新的蛇身节点
		if (cur == NULL)
		{
			perror("InitSnak()::malloc()");
			return;
		}

		//设置蛇身节点坐标，即让每个节点成为新的蛇头
		cur->x = pos_x + i * 2;  //每次新的节点x轴坐标都比前一个蛇身节点多2格
		cur->y = pos_y;
		cur->next = NULL;

		//使用头插法把蛇身节点都连接起来
		if (ps->_pSnakeHead == NULL)
		{
			ps->_pSnakeHead = cur;   //蛇身一个节点都没有的情况
		}
		else
		{
			cur->next = ps->_pSnakeHead;
			ps->_pSnakeHead = cur;
		}

	}

	//将蛇打印在地图上
	cur = ps->_pSnakeHead;
	while (cur)
	{
		SetPos(cur->x,cur->y);
		wprintf(L"%lc",BODY);
		cur = cur->next;
	}

}

//创建食物
void CreatFood(pSnake ps) {
	int x;
	int y;
	//为食物创建随机的坐标
again:
	do 
	{
		//食物的坐标要在墙体内
		x = rand() % 53 + 2;  //x轴的范围是 2 ~ 54
		y = rand() % 25 + 1;  //y轴的范围是 1 ~ 25
	} while (x % 2 != 0);

	//检查食物坐标是否与蛇身重叠
	pSnakeNode cur = ps->_pSnakeHead;
	while (cur) 
	{
		if ((cur->x == x) && (cur->y == y))
		{
			goto again;//若重叠，则重新为食物创建坐标
		}
		cur = cur->next;
	}

	//创建食物节点
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pFood == NULL)
	{
		perror("CreatFood()::malloc()");
	}
	pFood->x = x;
	pFood->y = y;
	pFood->next = NULL;

	//记录食物节点到贪吃蛇中
	ps->_pFood = pFood;

	//打印食物
	SetPos(x,y);
	wprintf(L"%lc", FOOD);
	
}

//一.游戏开始
void GameStart(pSnake ps) {
	//1.设置窗口大小，名称
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");

	//2.隐藏光标
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo = { 1,false }; //创建结构体，并赋值以你想要设置的控制台光标属性
	SetConsoleCursorInfo(hOutput, &CursorInfo);

	//3.打印欢迎界面
	WelcomeToGame();

	//4.地图绘制
	CreateMap();

	//5.将贪吃蛇属性部分初始化
	InitSnake(ps);

	//6.创建食物
	CreatFood(ps);

	/*getchar();*/
	
}
PrintfHelpInfo() {

	SetPos(62, 10);
	wprintf(L"%ls", L"不能穿墙，不能撞到自己");
	SetPos(62, 11);
	wprintf(L"%ls", L"按↑ . ↓ . ← . → 来控制蛇的移动");
	SetPos(62, 12);
	wprintf(L"%ls", L"F3加速 ，F4减速");
	SetPos(62, 13);
	wprintf(L"%ls", L"按Esc退出游戏，按空格暂停游戏");

	SetPos(62, 16);
	wprintf(L"%ls", L"———玖伍出品———");
}

//暂停
void Pause() {
	while (1) {
		Sleep(200);//为了省性能加上屏幕睡眠时间，否者一直死循环浪费新能
		if (KEY_PRESS(VK_SPACE))
		{
			break;
		}
	}
}

//判断下一个位置是不是食物
int NextIsFood(pSnakeNode pn, pSnake ps) {
	return ((pn->x == ps->_pFood->x) && (pn->y == ps->_pFood->y));
}

//吃掉食物
//pSnakeNode psn 是计算的下⼀个节点的地址
//pSnake ps 维护蛇的指针
void EatFood(pSnakeNode pn, pSnake ps) {

	//头插法，将即将要吃掉的食物节点成为新的头结点
	ps->_pFood->next = ps->_pSnakeHead;//食物节点成为新的头节点
	ps->_pSnakeHead = ps->_pFood;      //改变贪吃蛇的蛇头属性

	//打印蛇
	pSnakeNode cur = ps->_pSnakeHead;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%c", BODY);    //第一次循环就将原来的食物节点覆盖了，不用做过多的处理
		cur = cur->next;
	}

	//吃到食物加总分
	ps->_score += ps->_food_weight;

	//释放我们计算的下一个节点
	free(pn);
	pn = NULL;

	//创建新的食物
	CreatFood(ps);
}

//不是食物走一步
void NoFood(pSnakeNode pn, pSnake ps) {

	//走一步的过程：将计算的下一个节点头插，打印新的蛇（除了尾节点），最后在释放尾节点

	//将计算的下一个节点成为新的蛇头
	pn->next = ps->_pSnakeHead;
	ps->_pSnakeHead = pn;

	//打印的蛇头，覆盖食物的图案
	pSnakeNode cur = ps->_pSnakeHead;
	SetPos(cur->x, cur->y);
	wprintf(L"%lc", BODY);

	//找到新蛇倒数第二个节点，我们后面要让他成为新的尾节点，和释放旧的尾节点
	while (cur->next->next != NULL)
	{
		cur = cur->next;
	}
	
	//清除屏幕上残留的旧尾节点图案
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	//释放尾节点
	free(cur->next);
	cur->next = NULL;

	//将新为节点的next指向NULL
	cur->next = NULL;
}

//检测蛇是否撞到墙
void KillByWall(pSnake ps) {
	if (ps->_pSnakeHead->x == 0 || ps->_pSnakeHead->x == 56 ||
		ps->_pSnakeHead->y == 0 || ps->_pSnakeHead->y == 26)
	{
		ps->_status = KILL_BY_WALL;
	}
}

//检测蛇是否撞到自己
void KillBySelf(pSnake ps) {

	pSnakeNode cur = ps->_pSnakeHead->next;
	while (cur)
	{
		//若蛇头与某个蛇身体节点重叠就撞到自己了
		if (cur->x == ps->_pSnakeHead->x && cur->y == ps->_pSnakeHead->y)
		{
			ps->_status = KILL_BY_SELF;
			break;
		}
		cur = cur->next;
	}
}

//蛇走一步的过程
void SnakeMove(pSnake ps) {

	//计算蛇头的下一刻的位置
	pSnakeNode pNextNode = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNextNode == NULL)
	{
		perror("SnakeMove()::malloc");
	}

	switch (ps->_dir)
	{
	case UP:
		pNextNode->x = ps->_pSnakeHead->x;
		pNextNode->y = ps->_pSnakeHead->y - 1;
		break;
	case DOWN:
		pNextNode->x = ps->_pSnakeHead->x;
		pNextNode->y = ps->_pSnakeHead->y + 1;
		break;
	case RIGHT:
		pNextNode->x = ps->_pSnakeHead->x + 2;
		pNextNode->y = ps->_pSnakeHead->y;
		break;
	case LEFT:
		pNextNode->x = ps->_pSnakeHead->x - 2;
		pNextNode->y = ps->_pSnakeHead->y;
		break;
	}

	//判断下一个位置是不是食物

	if (NextIsFood(pNextNode,ps))//是食物此函数返回1，否者返回0
	{
		//下一个位置是食物，就吃掉
		EatFood(pNextNode, ps);
	}
	else
	{
		//下一个位置不是食物，蛇整体走一步
		NoFood(pNextNode, ps);
	}

	//检测蛇是否撞到墙
	KillByWall(ps);

	//检测蛇是否撞到自己
	KillBySelf(ps);
}
//2.游戏运行
void GameRun(pSnake ps) {

	//1.右侧打印帮助信息
	PrintfHelpInfo();

	//2.贪吃蛇在地图里行走，当蛇的状态！= OK时，结束行走
	do {

		//食物分数与总分数随着游戏的进行会被改变，所以要一直打印，直至游戏结束
		SetPos(64, 7);
		printf("总得分：%d ", ps->_score);
		SetPos(64, 8);
		printf("每个食物得分：%2d分", ps->_food_weight);

		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			//向上走的蛇方向不能是往下
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			//向下走的蛇方向不能是往上
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			//向左走的蛇方向不能是往右
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			//向右走的蛇方向不能是往左
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//暂停
			Pause();
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			//正常退出游戏
			ps->_status = END_NORMAL;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//加速
			if (ps->_sleep_time > 80)
			{
				ps->_sleep_time -= 30;
				ps->_food_weight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//减速
			if (ps->_food_weight > 2)
			{
				ps->_sleep_time += 30;
				ps->_food_weight -= 2;
			}
		}

		//蛇走一步的过程
		SnakeMove(ps);
		Sleep(ps->_sleep_time);

	}while(ps->_status == OK);//当状态不是OK时结束行走

}

//3.游戏结束
void GameOvre(pSnake ps) {
	SetPos(24, 12);
	switch (ps->_status)
	{
	case END_NORMAL:
		printf("您主动结束了游戏");
		break;
	case KILL_BY_WALL:
		printf("您撞上了墙，结束了游戏");
		break;
	case KILL_BY_SELF:
		printf("您撞上了自己，结束了游戏");
		break;
	}

	//释放链表
	pSnakeNode cur = ps->_pSnakeHead;
	while (cur)
	{
		pSnakeNode del = cur;
		cur = cur->next;
		free(del);
	}
}